package servlet.User;

import DAO.ArticleDAO;
import DAO.UserDAO;
import Model.User;
import appexception.AppException;
import service.BaseService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;

/**
 * @program: wsn_blog
 * @description
 * @author:王松年
 * @creat: 2021-07-13 09:41:11
 **/

@WebServlet("/deleteUser")
public class UserDeleteServlet extends BaseService{
    @Override
    protected void query(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        //从session获取用户
            HttpSession session = req.getSession(false);
            if (session == null) {
                throw new AppException("DELETE-USER001", "未登录");
            }
            User user = (User)session.getAttribute("user");
            if (user==null){
                throw new AppException("DELETE-USER002", "未登录");
            }
            ArticleDAO.deleteByUserId(user.getId());
            UserDAO.deleteById(user.getId());
            session.removeAttribute("user");
    }
}